<?php 
/*
 * form for creating new projects
*/
//check for a valid session
include("../inc_files/utils/checksession.php");
//include the page header
include("../inc_files/ui/header.php");?>
<title>New project</title>
<!-- include the datepicker javascript -->
<script type="text/javascript" src="../scripts/datepicker.js"></script>
<script type="text/javascript" src="../scripts/utils.js"></script>
<script type='text/javascript'>
function validateForm(){
	// reset the styles on the form fields to remove
	// any highlighted errors from previous submissions
	var pf=document.getElementById("projectform");		
	for (var i=0;i<pf.length;i++)
	  {
		  if(pf.elements[i].className = "tberror") pf.elements[i].className = "tbnormal"
	  }

	//check form validation
	var valid = true;
	var errormessage = "This form is not valid. Please correct the following errors.\n";

	//regular expression for dates in dd/mm/yyyy format
	var datereg = /(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/;

	//check the title field
	if(document.getElementById('title').value.length < 1){
		valid=false;
		document.getElementById('title').className = "tberror";
		errormessage += "\nTitle field empty";
	}
	//check the start date filed for not valid format
	var startdate = document.getElementById('startdate').value
	if(!startdate.match(datereg)){
		valid=false;
		document.getElementById('startdate').className = "tberror";
		errormessage += "\nStart date not valid (dd/mm/yyyy)";
	}
     
	//check the end date filed for not valid format
	var enddate = document.getElementById('enddate').value
	if(!enddate.match(datereg)){
		valid=false;
		document.getElementById('enddate').className = "tberror";
		errormessage += "\nEnd date not valid (dd/mm/yyyy)";
	}

	//check start date is after end date
	if(startdate.match(datereg) && enddate.match(datereg)){
		if(checkStartEndDates(startdate,enddate)){
			valid=false;
			document.getElementById('enddate').className = "tberror";
			document.getElementById('startdate').className = "tberror";
			errormessage += "\nEnd date is earlier than start date.";

		}
	
	}  

 	
	//check the customer drop down selection is made
	var customerid = document.getElementById('customerid').selectedIndex 
	if(customerid == 0){
		valid=false;
		document.getElementById('customerid').className = "tberror";
		errormessage += "\nPlease select a customer.";
	}

	//check the project manager drop down selection is made
	var projectmanager = document.getElementById('projectmanager').selectedIndex 
	if(projectmanager == 0){
		valid=false;
		document.getElementById('projectmanager').className = "tberror";
		errormessage += "\nPlease select a project manager.";
	}
	
	if(valid == false){
		alert(errormessage);
		
	}
	return valid;
}
</script>
</head>
<body>
	<script type="text/javascript" src="../scripts/datepicker.js"></script>
	<?php 	include("../inc_files/ui/menu.php");?>
	<form action="m_addproject.php" method="post" 
		id="projectform" onsubmit="return validateForm()">
		<table class="inputform">
			<tr>
				<td class="labelcell">Customer</td>
				<td class="formcell"><select name="customerid" id="customerid">
						<option value="Please select...">Please select...</option>
						<?php 	include("../inc_files/ui/customerddl.php");?>
				</select>
				</td>
			</tr>
			<tr>
				<td class="labelcell">Project title</td>
				<td class="formcell"><input type="text" id="title" name="title"
					size="54" /></td>
			</tr>
			<tr>
				<td class="labelcell">Start date</td>
				<td class="formcell"><input type="text" name="startdate"
					id="startdate" onfocus="return raise_picker_ui(this, 'dd/mm/yyyy')" />
				</td>
			</tr>

			<tr>
				<td class="labelcell">End date</td>
				<td class="formcell"><input type="text" name="enddate" id="enddate"
					onfocus="return raise_picker_ui(this, 'dd/mm/yyyy')" />
				</td>
			</tr>
			<tr>
				<td class="labelcell">Project manager</td>
				<td class="formcell"><select name="projectmanager"
					id="projectmanager">
						<option value="Please select...">Please select...</option>
						<?php 	include("../inc_files/ui/usersddl.php");?>
				</select>
				</td>
			</tr>
			<tr>
				<td class="labelcell">Project description:</td>
				<td class="formcell"><textarea rows="10" cols="40"
						name="projectdescription"></textarea>
				</td>
			</tr>
			<tr>
				<td class="separatorrow" colspan="2">&nbsp;</td>
			</tr>
			<tr>
				<td></td>
				<td><input type="submit" value="NEXT >>" /></td>
			</tr>
			<tr class="separatorrow">
				<td colspan="2"></td>
			</tr>
			<tr class="separatorrow">
				<td colspan="2"></td>
			</tr>
		</table>
	</form>
	<?php 	include("../inc_files/ui/footer.php");?>

</body>
</html>
